package com.songcha.sakura.ui.view.test

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import com.songcha.library_common.util.BitmapUtil

class TestXferView
@JvmOverloads
constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0,
    defRes: Int = 0
) : ViewGroup(context, attrs, defStyle, defRes) {

    private var mXferPaint= Paint()


    init {
        setWillNotDraw(false)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        for (i in 0 until childCount) {
            val child = getChildAt(i)
            measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0)
        }

        //measureChildWithMargins(child,widthMeasureSpec,0,heightMeasureSpec,0)
        //measureChildren(widthMeasureSpec,heightMeasureSpec)
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        if(childCount>0){
            val child=getChildAt(0)
            child.layout(0,0,child.measuredWidth,child.measuredHeight)

        }


    }


    override fun generateDefaultLayoutParams(): MarginLayoutParams? {
        return MarginLayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.MATCH_PARENT
        )
    }

    override fun generateLayoutParams(attrs: AttributeSet?): MarginLayoutParams? {
        return MarginLayoutParams(context, attrs)
    }

    override fun generateLayoutParams(lp: LayoutParams?): LayoutParams? {
        return MarginLayoutParams(lp!!)
    }

    override fun checkLayoutParams(p: LayoutParams?): Boolean {
        return p is MarginLayoutParams
    }




    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        if(canvas==null) return

        val saveCount = canvas.saveLayer(0f,0f,width.toFloat(),height.toFloat(),mXferPaint)

        val child=getChildAt(0)
           val childRect= Rect(0,0,child.width,child.height)
           val childBitmap= BitmapUtil.view2Bitmap(getChildAt(0),childRect,1f)
           canvas.drawBitmap(childBitmap!!,0f,0f,mXferPaint)
        child.visibility= View.INVISIBLE
        //canvas.drawBitmap(createDst(),0f,0f,mXferPaint)
           mXferPaint.xfermode= PorterDuffXfermode(PorterDuff.Mode.DST_ATOP)
           canvas.drawBitmap(createSrc(),0f,0f,mXferPaint)
           mXferPaint.xfermode=null


        canvas.restoreToCount(saveCount)

    }

    fun createDst():Bitmap{
        val bitmap = Bitmap.createBitmap(1000, 1000, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(bitmap)
        val paint=Paint()
        paint.color=Color.GREEN
        canvas.drawRect(0f,0f,1000f,1000f,paint)
        return bitmap
    }

    fun createSrc():Bitmap{
        val bitmap = Bitmap.createBitmap(1000, 1000, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(bitmap)
        val paint=Paint()
        paint.color=Color.YELLOW
        canvas.drawCircle(500f,500f,500f,paint)
        return bitmap
    }


}